package pl.game;

import org.anddev.andengine.entity.scene.menu.MenuScene;
import org.anddev.andengine.entity.scene.menu.animator.SlideMenuAnimator;
import org.anddev.andengine.entity.scene.menu.item.IMenuItem;
import org.anddev.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;



public class SubMenu extends MainMenu {
	// ===========================================================
	// Constants
	// ===========================================================

	private static final String TAG = "SubMenu";
	
	private static final int MENU_QUIT_OK = MainMenu.MENU_OPTIONS + 1;
	private static final int MENU_QUIT_BACK = MENU_OPTIONS + 2;

	// ===========================================================
	// Fields
	// ===========================================================

	private MenuScene mSubMenuScene;

	private BitmapTextureAtlas mSubMenuTexture;
	private TextureRegion mMenuOkTextureRegion;
	private TextureRegion mMenuBackTextureRegion;
	
	private MediaPlayer clickSound ;


	@Override
	public void onLoadResources() {
		
		super.onLoadResources();
		this.mSubMenuTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		this.mMenuOkTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mSubMenuTexture, this, "gfx/menu_ok.png", 0, 0);
		this.mMenuBackTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mSubMenuTexture, this, "gfx/menu_back.png", 0, 128);
		 Log.v(TAG, "Create Submenu");
		this.mEngine.getTextureManager().loadTexture(this.mSubMenuTexture);
		
		//ustawienie dzwieku
		clickSound = MediaPlayer.create(this, R.raw.button23);
	}

	@Override
	protected void createMenuScene() {
		super.createMenuScene();
		
		
		this.mSubMenuScene = new MenuScene(this.mCamera);
		this.mSubMenuScene.addMenuItem(new SpriteMenuItem(MENU_QUIT_OK, this.mMenuOkTextureRegion));
		this.mSubMenuScene.addMenuItem(new SpriteMenuItem(MENU_QUIT_BACK, this.mMenuBackTextureRegion));
		this.mSubMenuScene.setMenuAnimator(new SlideMenuAnimator());
		this.mSubMenuScene.buildAnimations();

		this.mSubMenuScene.setBackgroundEnabled(false);

		this.mSubMenuScene.setOnMenuItemClickListener(this);
	}

	@Override
	public boolean onMenuItemClicked(final MenuScene pMenuScene, final IMenuItem pMenuItem, final float pMenuItemLocalX, final float pMenuItemLocalY) {
		
		clickSound.start();
		switch(pMenuItem.getID()) {
			case MENU_START:
				// setContentView(new MapView(this));
				//this.mMainScene.reset();
				 Log.v(TAG, "PLAY Click");
					 
				// Odkomentowac
				this.startActivity(new Intent(this, pl.game.StartGameActivity.class));
				 
				// Intent questionsIntent = new Intent(this, pl.game.ShowQuestionActivity.class);
				// this.startActivityForResult(questionsIntent, 0);
				 
				//this.mMenuScene.closeMenuScene();
				return true;
			case MENU_OPTIONS:
				Log.v(TAG, "OPTIONS Click");
				 Intent optionsIntent = new Intent(this, pl.game.OptionsSubMenuActivity.class);
				 this.startActivityForResult(optionsIntent, 0);
				return true;
			case MENU_QUIT:
				pMenuScene.setChildSceneModal(this.mSubMenuScene);
				 Log.v(TAG, "QUIT Click");
				return true;
			case MENU_QUIT_BACK:
				this.mSubMenuScene.back();
				Log.v(TAG, "BACK Click");
				return true;
			case MENU_QUIT_OK:
				this.finish();
				return true;
			default:
				return false;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch (resultCode){
		case RESULT_OK:
		Bundle b = data.getExtras();
		String country = b.getString("countryName");
		String level = b.getString("levelDifficulty");
		String answer = b.getString("Answer");
		
		Log.v(TAG, "RESULT OPTIONS answer " + answer);
		Log.v(TAG, "RESULT OPTIONS country " + country);
		Log.v(TAG, "RESULT OPTIONS level " + level);
		} 
	}
	
}
